package com.huisang.controller;

import com.github.pagehelper.PageInfo;
import com.huisang.domain.Customer;
import com.huisang.service.CustomerService;
import com.huisang.util.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    /**
     * 查询所有
     * @param pageNum 当前页
     * @param pageSize 页大小
     * @return
     */
    @GetMapping("/cus")
    public ResponseEntity<PageInfo<Customer>> findAllCustomer(int pageNum, int pageSize) {
        PageInfo<Customer> customerAll = customerService.findCustomerAll(pageNum, pageSize);
        ResponseEntity<PageInfo<Customer>> responseEntity = new ResponseEntity<PageInfo<Customer>>();
        responseEntity.setCode(200);
        responseEntity.setMessage("OK");
        responseEntity.setData(customerAll);

        return responseEntity;
    }

    @GetMapping("/cus/findall")
    public ResponseEntity<List<Customer>> findOneCustomerByCid(){
        List<Customer> allCustomer2 = customerService.findAllCustomer2();
        ResponseEntity<List<Customer>> responseEntity = new ResponseEntity<>();
        responseEntity.setCode(200);
        responseEntity.setMessage("OK");
        responseEntity.setData(allCustomer2);
        return responseEntity;
    }

    /**
     * 添加
     * @param customer
     * @return
     */
    @PostMapping("/cus")
    public ResponseEntity<Integer> addCustomer(Customer customer) {
        ResponseEntity<Integer> responseEntity = customerService.addCustomer(customer);
        return responseEntity;
    }
    @DeleteMapping("/cus/{cid}")
    public ResponseEntity<Integer> deleteCustomer(@PathVariable("cid") Integer cid) {
        ResponseEntity<Integer> responseEntity = customerService.deleteCustomer(cid);
        return responseEntity;
    }
    @PutMapping("/cus")
    public ResponseEntity<Integer> updateCustomer(Customer customer) {
        ResponseEntity<Integer> responseEntity = customerService.updateCustomer(customer);
        return responseEntity;
    }
    @GetMapping("/cus/{cid}")
    public ResponseEntity<Customer> findOneCustomerById(@PathVariable("cid") Integer id) {
        ResponseEntity<Customer> responseEntity = customerService.findOneCustomerById(id);
        return responseEntity;
    }

}
